<template>
  <!-- 新增部门的弹层 -->
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    @close="$emit('update:showDialog', false)"
  >
    <!-- <el-dialog
    title="新增部门"
    :visible="showDialog"
    @update:showDialog="showDialog = $event"
  > -->
    <!-- <el-dialog title="新增部门" :visible="showDialog" :before-close="handleClose"> -->
    <!-- <el-dialog title="新增部门"> -->
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      label-width="120px"
      :model="formData"
      :rules="rules"
      ref="formData"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          style="width: 80%"
          placeholder="请选择"
          v-model="formData.manager"
          @focus="getEmployeeSimple"
        >
          <!-- 遍历选线 -->
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDepartments,
  addDepartments,
  getDeparDetail
} from '@/api/departments'
import { getEmployeeSimple, leditepartments } from '@/api/employee'
// import { constantRoutes } from '@/router'
export default {
  name: 'add-dept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async (rule, value, callback) => {
      // 先要获取最新的组织架构数据
      const { depts } = await getDepartments()
      // depts是所有的部门数据
      // 如何去找技术部所有的子节点

      const isRepeat = depts
        .filter((item) => item.pid === this.treeNode.id)
        .some((item) => item.name === value)

      isRepeat
        ? callback(new Error(`同级部门下已经有${value}的部门了`))
        : callback()
    }

    return {
      //   定义表单数据
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },

      peoples: [], //  请求的员工简单数据
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepeat },
          { min: 1, max: 50, message: '部门名称长度为1-50个', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '编码名称不能为空', trigger: 'blur' },

          { min: 1, max: 50, message: '部门编码长度为1-50个', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍长度为1-300个',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },
  methods: {
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      //   console.log(res)
      this.peoples = res
    },
    //  获取详情方法
    async getDeparDetail(id) {
      this.formData = await getDeparDetail(id)
      //  父传子
      //  props 传值是异步的
    },
    handleClose() {
      this.$emit('update:showDialog', false)
      // 清除之前的数据校验 重置表单

      this.$refs.formData.resetFields()
    },
    btnOk() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          if (this.formData.id) {
            await leditepartments(...this.formData)
          } else {
            //  是添加
            await addDepartments({
              ...this.formData,
              pid: this.treeNode.id || ''
            })
          }
          this.formData = {
            name: '',
            code: '',
            manager: '',
            introduce: ''
          }
          this.$emit('addDepts')
          this.$emit('update:showDialog', false)
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
</style>